<?php
//亚历山大 暂停使用
exit;
	ini_set("display_errors", "off");
	ini_set("memory_limit","1024M");
	set_time_limit(0);
	include_once(dirname(__FILE__).'/../include/lxbase.class.php');
	include_once(dirname(__FILE__).'/../include/wx.class.php');
 
        $wxfen = new Wx();
		//获取到所有的商家的信息 商家表是本地的。
		$user_data=$wxfen->arrdata("*","wxuser","where acctoken<>''");
		foreach ($user_data as $key => $value) {
			if($value['acctoken']){
					$user_id=$value['userid'];
					if(time()>$value["acctokentim"]){
						$view = $wxfen->viewdata("*","wxuser","where userid={$user_id}");
						$appid = $view["appid"];
						$secret = $view["secret"];
						if(empty($appid) || empty($secret)){
							continue;
						}
						$tokeninfo = $wxfen->get_access_token($appid,$secret);
						if(!empty($tokeninfo["access_token"])){
							$user_token = $tokeninfo["access_token"];
							$acctokentim = time()+$tokeninfo["expires_in"];
							$wxfen->updata("wxuser","acctoken='{$user_token}',acctokentim={$acctokentim}","where userid={$user_id}");
						}else{
							continue;
						}
					}else{
						$user_token=$value['acctoken'];
					}
					//获取微信端粉丝的openid数组信息
					$url="https://api.weixin.qq.com/cgi-bin/user/get?access_token=$user_token&next_openid=";
					$ret=json_decode($wxfen->call_get($url),true);			
					$openids=$ret['data']['openid'];
		
					//取出本地数据库的所有的粉丝信息
					$local_fans=$wxfen->arrdata("*","wxguest","where userid=$user_id");

					$local_openids=array();
					$local_fans_list=array();
					foreach ($local_fans as $k1 => $v1) {
						$local_openids[]=$v1['openid'];
						$local_fans_list[$v1['openid']]=$v1;
					}

					//获取微信粉丝的openid，acctoken是商家的，循环的获取粉丝的信息，放进数组当中
					foreach ($openids as $kk => $vv) {			
						$time_out=time() + 7*86400;
						$url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=$user_token&openid={$vv}&lang=zh_CN";
						$fans_info = json_decode($wxfen->call_get($url),true);

						if($fans_info){
							if($fans_info['sex']==1){
								$fans_info['sex']='男';
							}elseif($fans_info['sex']==2){
								$fans_info['sex']='女';
							}elseif($fans_info['sex']==0){
								$fans_info['sex']='未知';
							}
							if($fans_info['remark']==''){
								$fans_info['remark']=$fans_info['nickname'];
							}else{
								$fans_info['remark']=$fans_info['remark'];
							}
						//如果从微信中取出的openid在本地粉丝表里，但是时间过了 更新操作
						
						if(in_array($vv,$local_openids) && $local_fans_list[$vv]['tim']<$time_out){
							$set="uname='".addcslashes($fans_info['nickname'])."',remark='".addcslashes($fans_info['remark'])."',sex='".$fans_info['sex']."',province='".$fans_info['province']."',city='".$fans_info['city']."',uface='".$fans_info["headimgurl"]."',subscribe_time={$fans_info['subscribe_time']},tim={$time_out},groupid={$fans_info['groupid']}";
							$wxfen->updata("wxguest",$set,"where userid='{$user_id}' and openid='{$vv}'");

						}

						//如果不在本地openid里，却在微信里 是新增的，
						if(!in_array($vv, $local_openids)){
							$into="uid='{$value['token']}',openid='{$vv}',uname='".addcslashes($fans_info["nickname"])."',remark='".addcslashes($fans_info["remark"])."',uface='".$fans_info["headimgurl"]."'";
							$into.=",token='',rtoken='',tokentim=0,userid={$user_id},tim={$time_out},sex='".$fans_info['sex']."',province='".$fans_info["province"]."',city='".$fans_info["city"]."'";
							$into.=",subscribe_time={$fans_info['subscribe_time']},groupid={$fans_info['groupid']}";
							$wxfen->setdata("wxguest",$into);
							
						}
					}
												
				}
			}				
		}	
		echo "done";
		exit;